


//
//  CPJEmoticonTipView.swift
//  Sina
//
//  Created by YYY on 2017/9/12.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

import UIKit
import pop

/*
 //会根据图像大小设置图像视图的大小
 let image = UIImageView(image: UIImage(named: ""))
 */

class CPJEmoticonTipView: UIImageView
{
    
    //提示按钮
    private lazy var tipButton = UIButton()
    
    var preEmoticonModel: CPJEmoticonModel?
    
    //提示视图的模型
    var emoticonModel: CPJEmoticonModel?
    {
        didSet
        {
            
            if emoticonModel == preEmoticonModel
            {
                return
            }
            preEmoticonModel = emoticonModel
            
            //设置提示按钮表情数据
            tipButton.setTitle(emoticonModel?.emoji, for: .normal)
            tipButton.setImage(emoticonModel?.image, for: .normal)
            
            //设置提示按钮弹力动画
            //弹力动画的结束时间是根据弹力速度，自动计算的，不能也无法指定duration
            let springAnimation: POPSpringAnimation = POPSpringAnimation(propertyNamed: kPOPLayerPositionY)
            
            springAnimation.fromValue = 30
            springAnimation.toValue = 8
            //弹力速度
            springAnimation.springSpeed = 20
            //弹力弹性
            springAnimation.springBounciness = 20
            tipButton.layer.pop_add(springAnimation, forKey: nil)
            //tipButton.pop_add(springAnimation, forKey: nil)
            
            print("设置表情")
        }
    }
    
    //构造函数
    init() {
        
        let bundle = EmotionManager.shared.bundle
        let image = UIImage(named: "emoticon_keyboard_magnifier", in: bundle, compatibleWith: nil)
    
        super.init(image: image)
        
        //设置锚点
        layer.anchorPoint = CGPoint(x: 0.5, y: 1.2)
        
        addSubview(tipButton)
        
        tipButton.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
        tipButton.frame = CGRect(x: 0, y: 8, width: 36, height: 36)
        tipButton.setTitle("😄", for: .normal)
        tipButton.titleLabel?.font = UIFont.systemFont(ofSize: 32)
        tipButton.center.x = bounds.width * 0.5
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
